using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Menu : MonoBehaviour
{
    private List<LevelList> levelList;
    public GameObject prefab;
    [SerializeField] private SceneField LevelScenes;//关卡场景
    public Sprite[] sprites;
    void Start()
    {
        Time.timeScale = 1;
        levelList = DataManager.Instance.Load();
        
        //获取总星数
        int sumNum = 0;
        for (var i = 0; i < levelList.Count; i++)
        {
            sumNum += levelList[i].currentNum;
        }

        //图片单独渲染
        for (var i = 0; i < sprites.Length; i++)
        {
            GameObject go = Instantiate(prefab, transform.position, Quaternion.identity);
            go.transform.SetParent(transform, false);
            go.transform.Find("锁").GetComponent<Image>().sprite = sprites[i];
            go.transform.Find("锁").Find("文本").GetComponent<Text>().text = levelList[i].num.ToString();
            go.transform.Find("解锁").GetComponent<Image>().sprite = sprites[i];
            go.transform.Find("解锁").Find("文本").GetComponent<Text>().text = levelList[i].currentNum + "/" + levelList[i].list.Count * 3;
            if (sumNum < levelList[i].num)
            {
                go.transform.Find("锁").gameObject.SetActive(true);
            }
            else
            {
                go.transform.Find("解锁").gameObject.SetActive(true);
                LevelList list = levelList[i];
                int levelListIndex = i;
                go.transform.Find("解锁").GetComponent<Button>().onClick.AddListener(() => OnClick(list, levelListIndex));
            }
        }
        
    }

    void OnClick(LevelList levelList, int levelListIndex)
    {
        AudioManager.Instance.PlaySFX("点击");
        List.Instance.levelList = levelList;
        List.Instance.levelListIndex = levelListIndex;
        SceneManager.LoadScene(LevelScenes);
    }
}
